﻿using System;
using System.Collections.Generic;
using MyHealthByDesign.Domain;
using MyHealthByDesign.Properties;

namespace MyHealthByDesign.DataAccess.AccountDA
{
    public class AccountRepository
    {
        private string connString = Settings.Default.MyHealthByDesignDB;
        private AccountDao accountDao;
        public AccountRepository()
        {
            accountDao = new AccountDao(connString);
        }

        public User GetUser(string username, string password)
        {
            User user;

            try
            {
                user = accountDao.GetUserInfo(username, password);
            }
            catch (Exception ex)
            {
                user = null;
            }

            return user;
        }

        public string GetPassword(string email)
        {
            string password;

            try
            {
                password = accountDao.GetPassword(email);
            }
            catch (Exception ex)
            {
                password = string.Empty;
            }

            return password;
        }

        public int RegisterUser(string lastname, string firstname, string birthDate, string city, string zipCode, string email, string password, string mobile, string textFlag, string sex, string ethnicity)
        {
            int insertRowCount = 0;
            try
            {
                insertRowCount = accountDao.RegsiterUser(lastname, firstname, birthDate, city, zipCode, email, password, mobile, textFlag,
                                                          sex, ethnicity);
            }
            catch (Exception)
            {
                insertRowCount = 0;
            }

            return insertRowCount;
        }

        // this method decouples the data access from the application domain classes
        public List<Question> GetQuestions(int categoryID)
        {
            try
            {
                return accountDao.GetQuestionsForCategory(categoryID);
            }
            catch (Exception)
            {
                return  new List<Question>();
            }

        }

        public bool AnswersRecorded(List<QuestionAnswer> lstQa)
        {
            try
            {
                return accountDao.InsertUserAnswers(lstQa);
            }
            catch (Exception)
            {
                return false;
            }
        }

        /*
         * TODO: Need to clean up code in these methods. These methods were created to test if the stored procedure worked successfully.
         */
        public void GetUserAnswers(int userId)
        {
            try
            {
                //var count = accountDao.InsertUserAnswers(8, 1, "tbRestingHeartRate", "70");
                //var userAnswers = accountDao.GetUserAnswers(userId);
                //var count = accountDao.InsertOptionSpecial(19, "tbTest", "TextBox");
                /*var count = accountDao.InsertQuestion(4, 5, "MyHealthByDesign.Domain.Panels.CheckBoxList", "Test",
                                                      "TestData",
                                                      "Test", "Test", "Test","Test");*/
                //var count = accountDao.InsertQuestionOption(26, "Test", 6, "Test");
                /*var count = accountDao.UpdateQuestion(1, 2, 1, "MyHealthByDesign.Domain.Panels.CheckBoxList", "Test",
                                                      "Test", "Test", "Test", "Test");*/
//                var count = accountDao.UpdateQuestionActiveStatus(1);
//                var count = accountDao.UpdateQuestionOption(1, 2, "test");
//                var count = accountDao.UpdateQuestionOptionActiveStatus(1);
//                var count = accountDao.UpdateQuestionOptionSpecial(1, "Test");
                var count = accountDao.UpdateUserAnswers(2, "tbBMI", "80");
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}